package com.hzjh.base.ext.data

import android.annotation.SuppressLint
import android.provider.Settings
import com.blankj.utilcode.util.Utils
import com.hzjh.base.utils.eventbus.FlowBus
import com.hzjh.base.utils.function.toBean
import com.hzjh.base.utils.function.toJson
import com.hzjh.base.utils.other.KtDeviceUtils
import com.hzjh.base.utils.stroage.SKStorage
import java.util.UUID

/**
 * 用户信息持久化存储
 */
object UserConfigs {
    val oaid: String = KtDeviceUtils.getDeviceId()
    @SuppressLint("HardwareIds")
    val androidId:String = Settings.Secure.getString(
        Utils.getApp().contentResolver,
        Settings.Secure.ANDROID_ID
    )
    val ssId:String = UUID.randomUUID().toString()
    private var userDataBean: UserDataBean? = null
    fun saveUserData(userDataBean: UserDataBean?){
        userDataBean?.let {
            this.userDataBean = it
            SKStorage.put(Configs.KEY_USER_DATA, it.toJson())
            if(it.token?.isNotEmpty() == true){
                SKStorage.put(Configs.KEY_IS_LOGIN, true)
                //发送登录状态变更通知
                FlowBus.boolFlowBusNotify(FlowConst.REFRESH_LOGIN_STATUS,true)
            }
        }
    }
    fun getUserData():UserDataBean{
        if(userDataBean == null) {
            userDataBean = SKStorage.getString(Configs.KEY_USER_DATA, "")?.toBean<UserDataBean>()
        }
        return userDataBean?: UserDataBean()
    }

    private fun clearUserData(){
        userDataBean=null
        SKStorage.put(Configs.KEY_USER_DATA, "")
    }

    fun loginOut(){
        SKStorage.put(Configs.KEY_IS_LOGIN, false)
        clearUserData()
        //发送登录状态变更通知
        FlowBus.boolFlowBusNotify(FlowConst.REFRESH_LOGIN_STATUS,false)
    }

    fun isLogin():Boolean{
        return SKStorage.getBoolean(Configs.KEY_IS_LOGIN, false)
    }

}